package com.example.dormitory_manager.controller;


import com.example.dormitory_manager.entity.Student;
import com.example.dormitory_manager.form.SearchForm;
import com.example.dormitory_manager.form.StudentForm;
import com.example.dormitory_manager.service.StudentService;
import com.example.dormitory_manager.utils.ResultVOUtil;
import com.example.dormitory_manager.vo.ResultVo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-10-02
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/save")
    public ResultVo save(@RequestBody Student student) {
        Boolean flag = this.studentService.saveStudent(student);
        if (!flag) return ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }

    @GetMapping("/list/{page}/{size}")
    public ResultVo list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return ResultVOUtil.success(this.studentService.list(page, size));
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm){
        return  ResultVOUtil.success(this.studentService.search(searchForm));
    }

    @GetMapping("/findById/{id}")
    public ResultVo findById(@PathVariable("id") Integer id) {
        Student student=this.studentService.getById(id);
        StudentForm studentForm=new StudentForm();
        BeanUtils.copyProperties(student,studentForm);
        studentForm.setOldDormitoryId(student.getDormitoryId());
        return ResultVOUtil.success(studentForm);
    }

    @PutMapping("/update")
    public ResultVo update(@RequestBody StudentForm studentForm) {
        System.out.println(studentForm);
        Boolean update=this.studentService.update(studentForm);
        if(!update) return  ResultVOUtil.fail();
        return  ResultVOUtil.success(null);
    }

    @DeleteMapping("/delete/{id}")
    public ResultVo deleteById(@PathVariable("id") Integer id){
        Boolean delete=this.studentService.deleteById(id);
        if(!delete) return  ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }



}

